//
//  SNUserAccountViewModel.m
//  SinaVboc
//
//  Created by V on 29/11/2016.
//  Copyright © 2016 仁忄. All rights reserved.
//

#import "SNUserAccountViewModel.h"
#import "SNUserAccount.h"

@implementation SNUserAccountViewModel

+ (instancetype)shared {
    
    static SNUserAccountViewModel *_instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _instance = [[self alloc] init];
    });
    
    return _instance;
}

- (instancetype)init {
    if (self = [super init]) {
        
        //沙盒取值
        self.userAccount = [self loadUserInfo];
        
        NSString *urlString = _userAccount.avatar_large;
        
        _iconURL = [NSURL URLWithString:urlString];
    }
    
    return  self;
}


- (void)setUserAccount:(SNUserAccount *)userAccount {
    _userAccount = userAccount;
    
    NSString *urlString = userAccount.avatar_large;
    
    _iconURL = [NSURL URLWithString:urlString];
}


- (BOOL)isUserLogin {
    
    if (self.userAccount.access_token != nil && [self isExpires] == NO) {
        return  YES;
    }
    return  NO;
}

- (BOOL)isExpires {
    
    NSDate *expireDate = self.userAccount.expiresDate;
    NSDate *nowDate = [NSDate date];
    
    if ([expireDate compare:nowDate] == NSOrderedDescending) {
        return NO;
    }
    
    return  YES;
}

- (void)loadAccessToken:(NSString *)code finished:(void(^)(bool isSuccess))finished {
    
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *dict = @{@"client_id": client_id,
                           @"client_secret":client_secret,
                           @"grant_type" : @"authorization_code",
                           @"code":code,
                           @"redirect_uri":redirect_uri};
    
    [[SNNetworkTools sharedTools] request:POST urlString:urlString parameters:dict finished:^(id responseObject, NSError *error) {
        
        if (error) {
            NSLog(@"%@", error);
            finished(NO);
            return;
        }
        
        NSDictionary *dict = responseObject;
        
        //block传参
        [self loadUserInfo:dict finished:finished];
        
    }];
}



- (void)loadUserInfo:(NSDictionary *)dict finished:(void(^)(bool isSuccess))finished {
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *parameter = @{@"access_token" : dict[@"access_token"],
                                @"uid" : dict[@"uid"]};
    
    [[SNNetworkTools sharedTools] request:GET urlString:urlString parameters:parameter finished:^(id responseObject, NSError *error) {
        
        if (error) {
            finished(NO);
            return ;
        }
        
        NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithDictionary:responseObject];
        
        [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            [dictM setValue:obj forKey:key];
        }];
        
        SNUserAccount *userAccount = [[SNUserAccount alloc] initWithDict:dictM.copy];
        
        [self saveUserInfo:userAccount];
        
        self.userAccount = userAccount;
        
        finished(YES);
    }];
}



- (void)saveUserInfo:(SNUserAccount *)userAccount {
    NSLog(@"%@", kUsersInfoPath);
    [NSKeyedArchiver archiveRootObject:userAccount toFile:kUsersInfoPath];
}

- (SNUserAccount *)loadUserInfo {
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:kUsersInfoPath];
}

@end



















































